/**
 *
 */
package cn.joyfulmedia.common.dto;


import cn.joyfulmedia.common.result.CommonResultCodeEnum;
import cn.joyfulmedia.common.result.ResultCodeInterface;

public class ResultDO<T> extends BaseResultDO {

    private static final long serialVersionUID = -3434272908589805662L;

    private T data;

    public ResultDO() {
    }

    /**
     * 建议的方法
     *
     * @param resultCodeEnum
     * @param result
     */
    @Deprecated
    public ResultDO(ResultCodeInterface resultCodeEnum, boolean result) {
        setResultCode(resultCodeEnum.getResultCode());
        setErrorMessage(resultCodeEnum.getMessage());
        setSuccess(result);
    }

    /**
     * 复制一份result
     *
     * @param resultDo
     */
    @Deprecated
    public ResultDO(ResultDO resultDo) {
        this.errorMessage = resultDo.getErrorMesssage();
        this.setResultCode(resultDo.getResultCode());
        setSuccess(resultDo.isSuccess());
    }

    /**
     * 实例化ResultDO
     *
     * @param resultCodeEnum
     * @param result
     * @param errorType      异常类型 0.业务异常 1.底层系统级异常(db,redis,远程连接有问题,hsf等)
     */
    private ResultDO(ResultCodeInterface resultCodeEnum, boolean result, int errorType) {
        setResultCode(resultCodeEnum.getResultCode());
        setErrorMessage(resultCodeEnum.getMessage());
        setSuccess(result);
        setErrorType(errorType);
    }

    @Deprecated
    public ResultDO(T data) {
        this.data = data;
    }

    /**
     * 初始化ResultDO使用该方法
     *
     * @param <T>
     * @return
     */
    public static <T> ResultDO<T> buildResult() {
        return new ResultDO<>(CommonResultCodeEnum.SUCCESS, true, 0);
    }

    /**
     * 构建失败ResultDO
     * @param resultCodeInterface
     * @param <T>
     * @return
     */
    public static <T> ResultDO<T> buildFailResult(ResultCodeInterface resultCodeInterface) {
        return new ResultDO<>(resultCodeInterface, false, 0);
    }

    /**
     * 验证RPC返回体是否正常
     *
     * @param result 返回体
     * @return true:返回错误信息, false:正常返回信息
     */
    public static boolean isError(ResultDO result) {
        return !isSuccess(result);
    }

    /**
     * 验证RPC返回体是否正常
     *
     * @param result 返回体
     * @return true:正常返回信息, false:返回错误信息
     */
    public static boolean isSuccess(ResultDO result) {
        return result.isSuccess() || result.getResultCode() == CommonResultCodeEnum.SUCCESS.getResultCode();
    }

    @Deprecated
    public static <T> ResultDO<T> getResult() {
        return new ResultDO<T>();
    }

    public T getModule() {
        return data;
    }

    public void setModule(T data) {
        this.data = data;
    }

}
